
var popLayerArray = [];
var jsDebug = true;

function headroom_init() {
    var obj = document.querySelector('#header');
    if (!obj)
        return;
    // console.log(obj + " " + typeof(obj));
    var headroom = new Headroom(obj, {
        "tolerance": 5,
        "offset": 205,
        "classes": {
            "initial": "animated",
            "pinned": "slideDown",
            "unpinned": "slideUp"
        }
    });
    headroom.init();
}

function isset(val)
{
    if (typeof(val) === "undefined" || val === null || val === false)
        return false;
    return true;
}

function kyo_srv_handle(json) {
    if (jsDebug) {
        console.log('kyo srv handle url:' + json.url + ", tag : " + json.tag);
        // console.log('kyo srv handle ' + JSON.stringify(json));
    }

    if (isset(json.msg))
        layer.msg(json.msg);

    if (isset(json.close)) {
        layer.close(popLayerArray.pop());
    }

    if (isset(json.url)) {
        if (json.tag) {
            $.ajax({
                   url: json.url,
                   dataType: "json",
                   async: true,
                   success: function(data) {
                       if (!isset(data.tag))
                           data.tag = json.tag;
                       kyo_srv_handle(data);
                   },
                   error: function(err) {
                       layer.msg('连接失败，请重试!');
                   }
            });
        } else {
            location.href = json.url;
        }
    } else if (isset(json.data) && isset(json.tag)
                && isset(json.type) && json.type === "html") {
        $(json.tag).html(json.data);
    }

    if (isset(json.callback))
        eval(json.callback);
}


function kyo_link() {
    var link = JSON.parse('{' + $(this).attr('kyo-link') + '}');

    if (typeof(link) !== "object" || !isset(link.url)
            || (link.confirm && !confirm(link.confirm)))
        return false;

    if (!isset(link.ajax)) {
        var obj = $("#kyo_link");
        obj.attr("href", link.url);
        obj.attr("target", link.tag);
        $("#kyo_link_span").click();
        return false;
    }

    if (jsDebug) {
        console.log('link: ' + JSON.stringify(link));
    }

    $.ajax({
           url: link.url,
           dataType: "json",
           async: true,
           success: function(data) {
               if (!isset(data.tag))
                   data.tag = link.tag;
               kyo_srv_handle(data);
           },
           error: function(err) {
               layer.msg('link 连接失败，请重试!: ' + JSON.stringify(err));
           }
    });

    return false;
}

function kyo_pop() {
    var pop = JSON.parse('{' + $(this).attr('kyo-pop') + '}');

    if (typeof(pop) !== "object" || !isset(pop.url))
        return false;

    if (jsDebug) {
        console.log('pop: ' + JSON.stringify(pop));
    }

    var parea = "auto";
    if (isset(pop.w))
        parea = isset(pop.h) ? [pop.w + 'px', pop.h + 'px'] : pop.w + 'px';

    pop.title = isset(pop.title) ? pop.title : "";

    $.ajax({
           url: pop.url,
           dataType: "html",
           async: true,
           success: function(html) {
                var index = layer.open({
                    id: pop.id,
                    area: parea,
                    maxmin: true, 
                    type: 1,
                    title: pop.title,
                    content: html,
                    end: function() {
                        if (pop.bc !== null)
                            eval(pop.bc);
                    }
                });
                popLayerArray.push(index);
           },
           error: function(err) {
               layer.msg('连接失败，请重试!');
           }
    });
}

function kyo_submit() {
    var empty = false;
    $("input").each(function() {
        if ($(this).attr('required') && $(this).val().length == 0) {
            empty = true;
            layer.msg($(this).attr('placeholder') + " 字段不能为空!");
            $(this).focus();
        }
    });
    if (empty)
        return false;

    if (jsDebug) {
        console.log('form action: ' + $(this).attr("action"));
        console.log('form post : ' + $(this).serialize());
    }

    $.post($(this).attr("action"), $(this).serialize(), success, "json");

    function success(data) {
        kyo_srv_handle(data);
    }

    return false;
}

$(document).ready(function() {

    headroom_init();
    $(document).off('submit', 'form[kyo-ajax]');
    $(document).on('submit', 'form[kyo-ajax]', kyo_submit);
    $(document).off('click', '[kyo-pop]');
    $(document).on('click', '[kyo-pop]', kyo_pop);
    $(document).off('click', '[kyo-link]');
    $(document).on('click', '[kyo-link]', kyo_link);

});

